package org.senseframework.support.orm.hbase.serializer;

import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;

/**
 * @author: yoqu
 * @date: 2018/9/29
 * @email: yoqulin@qq.com
 **/
public class JacksonSerialize implements ISerialize {
    ObjectMapper objectMapper;

    public JacksonSerialize() {
        objectMapper = new ObjectMapper();
    }

    @Override
    public byte[] encode(Object obj) {
        try {
            return objectMapper.writeValueAsBytes(obj);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("jacksonSerialize encode fail. {}",e);
        }
    }

    @Override
    public Object decode(byte[] bytes, Class tClass) {
        try {
            return objectMapper.readValue(bytes,tClass);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("jacksonSerialize decode fail. {}",e);
        }
    }
}
